package com.eece496.Database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Classroom extends Database{

	private int roomNumber; 
	private String buildingName;
	private String className;
	
	public Classroom()
	{
		tableName = "Classroom";	
	}
	
	public Classroom(int roomNumber, String buildingName, String classNumber)
	{
		tableName = "Classroom";
		
		this.roomNumber = roomNumber;
		this.buildingName = buildingName;
		this.className = className;
	}
	
	/*
    //@assignable stmt,sqlString;
	//@signals_only RuntimeException, IOException, SQLException;
	public boolean UserExists(int id) throws SQLException
	{
		Statement stmt = connection.createStatement();
		String sqlString = "select * from " + tableName + " where roomNumber = " + id;
		
		ResultSet rs = stmt.executeQuery(sqlString);
		
		if (rs.next())//check if user exists in databse
		{
			this.roomNumber = Integer.parseInt(rs.getString("roomNumber"));
			this.buildingName = rs.getString("buildingname");
			this.className = rs.getString("classname");
			return true;
		}
		else
		{
			return false;
		}
	}
	*/
	//@requires buildingName !== Null;
    //@assignable newBuildingname,roomNumber;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModBuildingName(String newBuildingName, int tupleID) throws SQLException
	{
		super.Modify(newBuildingName, "buildingname","roomNumber",tupleID); 
	}
	//@requires  className !== Null;
    //@assignable newClassName,roomNumber;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModClassName(String newClassName, int tupleID) throws SQLException
	{
		super.Modify(newClassName, "className","roomNumber",tupleID); 
	}
	
    //@assignable stmt,sqlString;
	//@signals_only RuntimeException, IOException,SQLException;
	@Override
	public void Insert() throws SQLException 
	{
		Statement stmt = connection.createStatement();
				
	    String sqlString = "INSERT INTO " + tableName + " VALUES (" + 
	    					roomNumber + ", " +
	    					AddQuotes(buildingName) + ", " +
	    					AddQuotes(className) + ", " +
	    					")";
	    
	    stmt.executeUpdate(sqlString);
	        
	    System.out.println(sqlString + " complete");
	}
	
	@Override
    //@assignable stmt,id,sqlString;
	//@signals_only RuntimeException, IOException,SQLException;
	public void Delete(String primaryKey) throws SQLException 
	{
		Statement stmt = connection.createStatement();
		int id = Integer.parseInt(primaryKey);
		String sqlString = "DELETE from " + tableName + " WHERE roomNumber = " + id;
		stmt.executeUpdate(sqlString);
		System.out.println(sqlString + " complete");
	}
	
	@Override
    //@assignable sqlString, stmt, ResultSet;
	//@signals_only RuntimeException, IOException,SQLException;
	public void Display() throws SQLException 
	{
	    String sqlString = "SELECT * FROM " + tableName;
	    Statement stmt = connection.createStatement();

	    ResultSet rs = stmt.executeQuery(sqlString);

	    while (rs.next())
	    {
	        System.out.println
	        (
	            rs.getString("roomNumber") + " " +
                    rs.getString("buildingName") + " " +
	            rs.getString("classNumber") + " " 
	        );
	    }	
	}

}